package com.kjlink.widget.monitoring.test.model;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.kjlink.widget.monitoring.enums.EventTypeEnum;
import com.kjlink.widget.monitoring.enums.MonitorTypeEnum;
import com.kjlink.widget.monitoring.model.BaseMonitorDataExtraModel;
import com.kjlink.widget.monitoring.model.MonitorDataModel;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Getter;
import lombok.Setter;
import org.springframework.beans.BeanUtils;

import javax.validation.constraints.NotEmpty;
import java.time.LocalDateTime;

/**
 * 描述:
 *
 * @author Vic.xu
 * @since 2025-08-11 10:33
 */
@Getter
@Setter
@Schema(name = "MockProduceModel", description = "测试生成监控事件")
public class MockProduceModel {

    @Schema(description = "事件类型", requiredMode = Schema.RequiredMode.REQUIRED,
            allowableValues = {"CREDENTIAL_TRANSFER_FAIL", "PROCESS_ENGINE_ERROR", "YONYOU_CONN_FAIL", "CLOUD_FINANCE_CONN_FAIL"})
    private EventTypeEnum eventType;

    @NotEmpty
    @Schema(description = "业务类型", defaultValue = "test")
    private String BusinessType;

    @NotEmpty
    @Schema(description = "监控类型", defaultValue = "OPEN_PLATFORM", allowableValues = {"HOST_APP", "OPEN_PLATFORM", "BANK_SELF_DEVELOP"})
    private MonitorTypeEnum monitorType = MonitorTypeEnum.OPEN_PLATFORM;

    @NotEmpty
    @Schema(description = "创建人", defaultValue = "admin")
    private String createdBy;

    @NotEmpty
    @Schema(description = "错误代码", defaultValue = "NPE")
    private String eventCode;

    @NotEmpty
    @Schema(description = "发生地点", defaultValue = "192.168.1.1")
    private String occurPlace;

    /**
     * 事件唯一id
     */
//    @Schema(description = "事件唯一id", accessMode = Schema.AccessMode.READ_ONLY)
//    private String eventId;

    /**
     * 事件 关联的业务id
     */
    @NotEmpty
    @Schema(description = "事件 关联的业务id")
    protected String businessId;

    /**
     * 事件 关联的业务类型
     */
    @NotEmpty
    @Schema(description = "事件 关联的业务类型")
    protected String businessType;

    /**
     * 事件 发生时间
     */

    @Schema(description = "事件 发生时间(不填写则当前时间)", nullable = true)
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    protected LocalDateTime occurTime = LocalDateTime.now();

    /**
     * 交易代码
     */
    @NotEmpty
    @Schema(description = "交易代码")
    protected String tranCode;

    /**
     * 资源代码
     */
//    @NotEmpty
//    @Schema(description = "资源代码")
//    protected String resourceCode;

    /**
     * 业务异常情况（非必填）<br>
     * 说明: 异常详细说明，如"余额不足"
     */
    @Schema(description = "业务异常情况（非必填）")
    private String exception;

    /**
     * 业务源应用（非必填）<br>
     * 说明: 业务链路起始应用ID，如"SRC001"
     */
    @Schema(description = "业务源应用（非必填）")
    private String sourceAppcode;

    /**
     * 业务目标应用（非必填）<br>
     * 说明: 业务链路目标应用ID，如"DEST001"
     */
    @Schema(description = "业务目标应用（非必填）")
    private String targetAppcode;


    /**
     * 两者字段完全相同, 所以使用 BeanUtils 转换
     */
    public MonitorDataModel<? extends BaseMonitorDataExtraModel> toMonitorDataModel() throws InstantiationException, IllegalAccessException {
        MonitorDataModel<BaseMonitorDataExtraModel> model = new MonitorDataModel<>();

        // 复制所有同名同类型字段（忽略extra，因为MockProduceModel中没有）
        BeanUtils.copyProperties(this, model);

        // 测试阶段给extra设一个默认空实现
        BaseMonitorDataExtraModel extraModel = model.getEventType().getExtraClass().newInstance();
        model.setExtra(extraModel);

        return model;
    }

}
